<?php
namespace Home\Biz;

/**
 * Description: OTMS方法类
 * @version 2016年10月24日 上午11:27:21 
 * @author lin.yujiao@whaley.cn
 */
class OtmsBiz {
	
	/**
	 * Description: 获取物流信息与物流事件包装成wi-tree-grid可支持模式
	 * @version 2016年10月21日 下午5:05:31 
	 * @author lin.yujiao@whaley.cn
	 * @return mixed
	 */
	public static function logistics4TreeGrid($tracker_no){
		$otms_prefix_path = C('PREFIX_PATH.OTMS');
		$logistics_lst = self::findDeliveryInfo($tracker_no);
		if(sizeof($logistics_lst) > 0){
			//加载运单事件类型以及单位类型映射配置
			$tms_event_type = CommonBiz::getOptions("TMS_EVENT_TYPE");
			$tms_unit_type = CommonBiz::getOptions("TMS_UNIT_TYPE");
			$event_lst = array();
			foreach ($logistics_lst as $index => $delivery){
				//事件
				$event_lst = self::findTmsEventInfo($delivery['trans_no']);
				//产品
				$tms_sku_lst = self::findSkuInfo($delivery['id']);
				//第二级数量
				$second_size = sizeof($event_lst) + sizeof($tms_sku_lst);
				if($second_size > 0){
					//循环处理事件
					foreach ($event_lst as $event_index => $event){
						//根据运单事件类型映射配置替换运单事件类型显示
						$event_lst[$event_index]['event_type_cn'] = $tms_event_type[$event['event_type']];
						$trim_file_name = trim($event['file_name']);
						if(!empty($trim_file_name)){
							$file_lst = explode(';', $trim_file_name);
							if(sizeof($file_lst) > 0){
								$event_lst[$event_index]['file_lst'] = array();
								foreach ($file_lst as $file_index => $path){
									array_push($event_lst[$event_index]['file_lst'], ($otms_prefix_path . $path));
								}
							}
						}
					}
					//循环处理产品
					foreach ($tms_sku_lst as $sku_index => $sku){
						//根据运单事件类型映射配置替换运单事件类型显示
						$tms_sku_lst[$sku_index]['unit_type_cn'] = $tms_unit_type[$sku['unit_type']];
					}
					$logistics_lst[$index]['event_list'] = $event_lst;
					$logistics_lst[$index]['sku_list'] = $tms_sku_lst;
					$logistics_lst[$index]['second_size'] = $second_size;
					$logistics_lst[$index]['sku_size'] = sizeof($tms_sku_lst);
					$logistics_lst[$index]['event_size'] = sizeof($event_lst);
				}else{
					$logistics_lst[$index]['second_size'] = 0;
					$logistics_lst[$index]['sku_size'] = 0;
					$logistics_lst[$index]['event_size'] = 0;
				}
				$logistics_lst[$index]['trans_status_cn'] = $tms_event_type[$delivery['trans_status']];
			}
		}else{
			$logistics_lst = array();
		}
		return $logistics_lst;
	}
	/**
	 * Description: 根据追踪号获取物流信息
	 * @version 2016年10月21日 下午5:09:36 
	 * @author lin.yujiao@whaley.cn
	 * @param unknown $alloc_order_no
	 */
	private static function findDeliveryInfo($tracker_no){
		$condition_array = array();
		$condition_array['tracker_no'] = array('eq',$tracker_no);
		return M('tms_delivery_info', '', 'DB_whaley_wms' )-> where($condition_array)-> select();
	}
	/**
	 * Description: 根据运单ID获取运单详情
	 * @version 2016年10月21日 下午5:09:36 
	 * @author lin.yujiao@whaley.cn
	 * @param unknown $alloc_order_no
	 */
	private static function findSkuInfo($tms_delivery_id){
		$condition_array = array();
		$condition_array['tms_delivery_id'] = array('eq',$tms_delivery_id);
		return M('tms_sku_item', '', 'DB_whaley_wms')-> where($condition_array)-> select();
	}
	/**
	 * Description: 根据运单号获取运单事件
	 * @version 2016年10月21日 下午5:09:36 
	 * @author lin.yujiao@whaley.cn
	 * @param unknown $alloc_order_no
	 */
	private static function findTmsEventInfo($trans_no){
		$condition_array = array();
		$condition_array['otms_order_no'] = array('eq',$trans_no);
		return M('tms_event_info', '', 'DB_whaley_wms')-> where($condition_array)-> select();
	}
	
} 